package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.ticket.dao.TicketProvincesDao;
import ysq.xyg.ticket.model.po.TicketProvinces;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.po.TicketSupplier;
import ysq.xyg.ticket.model.vo.TicketProvincesVo;
import ysq.xyg.ticket.model.vo.TicketSupplierVo;
import ysq.xyg.ticket.service.TicketProvincesService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 票卷模块-省市区service实现
 *
 * @author shengQiang yu
 * @date 2019-10-23 16:06:37
 */
@Service("ticketProvincesService")
public class TicketProvincesServiceImpl extends BaseServiceImpl<TicketProvinces> implements TicketProvincesService {

    @Resource
    private TicketProvincesDao ticketProvincesDao;

    private TicketProvinces ticketProvinces;

    @Autowired
    public void setBaseDao(TicketProvincesDao ticketProvincesDao) {
        this.baseDao = ticketProvincesDao;
    }


    /**
     * 分页查询
     *
     * @param ticketProvincesVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    public ReturnEntity page(TicketProvincesVo ticketProvincesVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<TicketProvinces> list = acceptList(ticketProvincesVo);
        PageInfo<TicketProvinces> page=new PageInfo<TicketProvinces>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 集合查询
     *
     * @param ticketProvincesVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    public ReturnEntity list(TicketProvincesVo ticketProvincesVo) {
        PageHelper.orderBy(" update_time desc");
        List<TicketProvinces> list = acceptList(ticketProvincesVo);
        return new ReturnEntity(list);
    }
    /**
     * 修改
     *
     * @param ticketProvincesVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    public ReturnEntity addOrUpdate(TicketProvincesVo ticketProvincesVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketProvincesVo.getId())){
            ticketProvinces=new TicketProvinces(currentUser);
            isUpate=false;
        }else{
            ticketProvinces=selectByPrimaryKey(ticketProvincesVo.getId());
            if(ObjectUtil.isNull(ticketProvinces)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketProvinces.setUpdateTime(new Date());
            ticketProvinces.setUpdateUser(currentUser.getId());
        }
        ticketProvinces.setName(ticketProvincesVo.getName());
        ticketProvinces.setProvincesType(ticketProvincesVo.getProvincesType());
        ticketProvinces.setParentId(ticketProvincesVo.getParentId());
        ticketProvinces.setSort(ticketProvincesVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketProvinces);
        }else{
            num=insert(ticketProvinces);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketProvinces ticketProvinces=selectByPrimaryKey(id);
        if(ticketProvinces!=null){
            ticketProvinces.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketProvinces.setUpdateTime(new Date());
            ticketProvinces.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketProvinces);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketProvincesVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    public List<TicketProvinces> acceptList(TicketProvincesVo ticketProvincesVo){
        Example example = new Example(TicketProvinces.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketProvincesVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketProvincesVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(ticketProvincesVo.getProvincesType())){
            criteria.andEqualTo("provincesType",ticketProvincesVo.getProvincesType());
        }
        if(StringUtil.isNotEmpty(ticketProvincesVo.getParentId())){
            criteria.andEqualTo("parentId",ticketProvincesVo.getParentId());
        }
        if(StringUtil.isNotEmpty(ticketProvincesVo.getName())){
            criteria.andLike("name","%"+ticketProvincesVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return ticketProvincesDao.selectByExample(example);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @Override
    public ReturnEntity acceptSelect(String code,TicketProvincesVo ticketProvincesVo) {
        List<TicketProvinces> list = acceptList(ticketProvincesVo);
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(TicketProvinces ticketProvinces:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(ticketProvinces.getId());
            sysDictContent.setName(ticketProvinces.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(ticketProvinces.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
